!function () {
    getQuery = function (variable) {
        var query = window.location.search.substring(1);
        var vars = query.split('&');
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split('=');
            if (decodeURIComponent(pair[0]) == variable) {
                return decodeURIComponent(pair[1]);
            }
        }
    };

    const max_histories = 20;

    let getHistory = function(){
        return localStorage.test_history
    };

    let setHistory = function(history){
        localStorage.test_history = history
    };

    pushHistory = function (id, title, logo) {
        let histories = getHistory();
        if(!histories) {
            histories = "[]";
        }
        let jsonData = JSON.parse(histories);
        let exist = false;
        for (let key in jsonData) {
            if(jsonData[key].id === id) {
                exist = true;
                break
            }
        }
        if(!exist) {
            jsonData.push({
                id: id,
                title: title,
                logo: logo
            });
            if (jsonData.length > max_histories) {
                jsonData.splice(0, 1)
            }
            setHistory(JSON.stringify(jsonData))
        }
    };

    readHistories = function () {
        let histories = getHistory();
        if(!histories) {
            histories = "[]";
        }
        return JSON.parse(histories);
    };

    deleteHistory = function (subject_id) {
        let histories = getHistory();
        if(!histories) {
            histories = "[]";
        }
        let jsonData = JSON.parse(histories);
        for (let key in jsonData) {
            if(jsonData[key].id === subject_id){
                jsonData.splice(key, 1)
            }
        }
        setHistory(JSON.stringify(jsonData))
    }
}();